#include <stdio.h>
#include <string.h>

typedef struct Student{
    int no;
    char name[10];
    float score;
}Student;

int main(){
    FILE* fp = fopen("03_objfile","wb");
    if(!fp){
        printf("Error opening file\n");
        return -1;
    }

    Student students[]={{1001,"zack",100},{1002,"ray",99.5}};
    int obj_count = fwrite(students,sizeof(Student),2, fp);
    if(obj_count < 0){
        printf("Error writing to file\n");
        return -1;
    }

    fclose(fp);
    
    FILE* fp_read = fopen("03_objfile","rb");
    if(!fp_read){
        printf("Error opening file\n");
        return -1;
    }

    printf("Reading from file\n");

    Student students_read[5];
    int read_count = fread(students_read,sizeof(Student), 5, fp_read);
    printf("read count is %d\n", read_count);
    for(int i = 0; i < read_count; i++){
        printf("%d %s %.2f\n",students_read[i].no, 
        students_read[i].name, students_read[i].score);
    }

    
    fclose(fp_read);
}